/*
 * 文件名：UserDetailService.java
 * 版权：Copyright by www.nd.com
 * 描述：
 * 修改人：Administrator
 * 修改时间：2016年10月18日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package com.wmzdq.security;

import java.security.Principal;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.wmzdq.common.IMenuService;
import com.wmzdq.common.IRoleMenuService;
import com.wmzdq.common.IRoleService;
import com.wmzdq.common.IUserService;
import com.wmzdq.dto.Tmenu;
import com.wmzdq.dto.Trole;
import com.wmzdq.dto.TroleMenu;
import com.wmzdq.dto.Tuser;


public class UserDetailService  implements UserDetailsService
{

    @Autowired
    IUserService userService;
    @Autowired
    IRoleMenuService roleMenuService;
    
    @Autowired
    IRoleService roleService;
    
    @Autowired
    IMenuService menuSerivce;
    
    public  UserDetailService(IUserService userService,IRoleMenuService roleMenuService,IRoleService roleService,IMenuService menuService) {
		// TODO Auto-generated constructor stub
    	this.userService=userService;
    	this.roleMenuService=roleMenuService;
    	this.roleService=roleService;
    	this.menuSerivce=menuService;
	}
    
    @Override
    @Transactional
    public UserDetails loadUserByUsername(String username)
        throws UsernameNotFoundException
    {
        Tuser user=userService.getByUsername(username);
        if(user==null)
            throw new UsernameNotFoundException("用户不存在");
        
        //获取用户角色
        String id=user.getRoleid();
        if(id==null)
            throw new UsernameNotFoundException("用户无角色");
        
        Trole role=roleService.getById(id);
        if(role==null)
            throw new UsernameNotFoundException("用户无角色");
        
        
        Collection<SimpleGrantedAuthority> grandteds=obtionGrantedAuthorities(user, role);
        
        UserDetailsImpl impl=new UserDetailsImpl(user.getUsername(), user.getPwd(), grandteds);
       
        impl.setUserEntity(user);
        impl.setRole(role);
        return impl;
    }
    
    
    //获取用户所有权限,如果权限控制粒度 要按钮，可以新增一个功能字段，重写一个grantedAuthority类
  //  @Transactional
    private Set<SimpleGrantedAuthority> obtionGrantedAuthorities(Tuser user,Trole role){
        Set<SimpleGrantedAuthority> set=new HashSet<SimpleGrantedAuthority>();
        
        List<TroleMenu> list=roleMenuService.queryByRoleId(new Long(role.getId()));
        
        for(TroleMenu roleMenu:list){
            Tmenu menu=menuSerivce.getById(roleMenu.getMenuid());
            set.add(new SimpleGrantedAuthority(menu.getUrl()));
        }
        
        return set;
    }

}
